/**
 * @author abid.hussain
 */

exports.GAConnectHandler = Core.Class.subclass({
	
	classname : 'GAConnectHandler',
	
	initialize : function(url, delegate)
	{
		this._delegate = delegate;
		this._webFetcher = null;
		this._responseObject = null;
		this._url = url;
	},
	fetchResponse : function(){
		this._webFetcher = new GAWebFetcher(this, 30, 0);
		this._webFetcher.invokeGetRequest(this._url);
	},
	webFetchSuccess : function(webFetcher, responseData){
		this._delegate.showProgress(kGAGameLoadLevel.kGAGameLoadLevelStart);
		try{
			var jsonResponse = eval("(" + responseData + ")");
			//var serverResponse = jsonResponse.ServerResponse;
			
			//Custom Modifications for Saving updated serverTime to Constant Servertime variable
			var serverTime = jsonResponse.game.game.server_time;
			var neighborActions = jsonResponse.user.user.nactions;
			
			
			var actionsArray = [];
			var actions = null;
			
			for(var i = 0; i < neighborActions.length; i++)
			{
				actions = neighborActions[i].naction.act_json;
				actions = eval("(" + actions + ")");
				//actions = JSON.stringify(actions);
				console.log("zaki:::::: actions data is "+actions.data.length);
				var action = null;
				for (var j = 0; j < actions.data.length; j++)
				{
					console.log("zaki:::::: actions data is "+actions.data[j]);
					action = actions.data[j];
					action = eval('('+action+')');
					actionsArray.push(action);
				}
				
			}

			var connectResponse = kConnectResponse;
			var serverResponse = connectResponse._serverResponse;
			serverResponse['@servertime'] = serverTime;
			serverResponse['neighboractions'] = actionsArray;
//			console.log("Zuluuu::: Json Response Status "+jsonResponse['status']);
			
			
			if(jsonResponse['status'] != true){
				this._delegate.connectHandlerFailure("Server Response is not ok with value of status = "+jsonResponse['status']);
			}
			else{
				this._responseObject= new GAConnectResponse(serverResponse);
				this._responseObject.parseConnectResponse();
				this._responseObject._serverXp = jsonResponse.user.user.xp;
				//this._responseObject._serverXp = 1;
				this._delegate.connectHandlerSuccess(this._responseObject);
			}
		}catch(exception){
			this._delegate.connectHandlerFailure(exception);
		}
		if(webFetcher != null){
			webFetcher.destroy();
			webFetcher = null;
		}
	},
	webFetchFailure : function(webFetcher, errorCode){	
		var errorMsg = "GAConnectHandler::webFetchFailure() responseStatusCode = > " + errorCode ;
		GALogger.logWithLevel(errorMsg,LOG_LEVEL.LOG_NONFATAL_ERROR);
		this._delegate.connectHandlerFailure(errorCode);
		if(webFetcher != null){
			webFetcher.destroy();
			webFetcher = null;
		}
	},
	getResponseObject : function (){
		return this._responseObject;
	},
	destroy : function(){
		this._delegate = null;
		if(this._webFetcher != null){
			this._webFetcher.destroy();
			this._webFetcher = null;
		}
		if(this._responseObject != null){
			this._responseObject.destroy();
			this._responseObject = null;
		}
		this._url=null;
		
	}
});